#pragma once
#include "generator.hpp"
#include "sound_out.hpp"

template <typename Generator>
class SquareSynth
{
public:
    SquareSynth();
    float operator()();
private:
    Generator in_;
    float accumulator_;
};


template <typename Generator>
SquareSynth<Generator>::SquareSynth():
    accumulator_(0)
{}

template <typename Generator>
float SquareSynth<Generator>::operator()()
{
    const float t = SAMPLE_RATE / in_();
    accumulator_ += 1;
    if (accumulator_ > t)
        accumulator_ -= t;
    return (accumulator_ > t / 2) ? 1 : -1;
}
